﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using CLab.Base;
using GandA.Component.Log;

namespace CLab.PagAdminLab
{
    public partial class CatEstudios : PageBase
    {
        #region Eventos

        protected void Page_Init(object sender, EventArgs e)
        {
            IdItem = 6;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    InicializaPantalla();
                    //CargaGrid();
                }
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void BtnBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                CargaGrid();
                if (gridEstudios.Rows.Count == 0)
                {
                    Master.alertSinDatos();
                }

                SetFocus(txtNombre);
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }
        protected void gridEstudios_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridEstudios.PageIndex = e.NewPageIndex;
            CargaGrid();
        }

        protected void btnExportar_Click(object sender, EventArgs e)
        {
            try
            {
                gridEstudios.AllowPaging = false;
                CargaGrid();
                gridEstudios.ToExportCsv();
                gridEstudios.AllowPaging = true;
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        
        protected void gridEstudios_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string[] sParams;
                switch (e.CommandName)
                {
                    case "cmdModificar":
                        sParams = e.CommandArgument.ToString().Split(',');
                        Response.Redirect(ResolveUrl("CatEstudiosRegistro.aspx?IdEstudio=" + sParams[0]), false);
                        break;
                    //case "cmdEliminar":
                    //    sParams = e.CommandArgument.ToString().Split(',');
                    //    EliminaEstudio(CLab.Common.Util.Convert.ToNInt(sParams[0]));
                    //    break;
                    case "cmdCampo":
                        sParams = e.CommandArgument.ToString().Split(',');
                        Response.Redirect("CatCampoEstudio.aspx?IdEstudio=" +  CLab.Common.Util.Convert.ToNInt(sParams[0]), false);
                        break;
                }
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
           
        }

        protected void gridEstudios_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gridEstudios.PageIndex = e.NewPageIndex;
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        protected void btnElimina_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Common.Util.Convert.ToNInt(hdnId.Value).GetValueOrDefault(-1);
                EliminaEstudio(id);
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex, "Error en: btnElimina_Click");
                Master.alert(ex.Message);
            }
        }
        #endregion

        #region Métodos
        private void InicializaPantalla()
        {
            Master.TituloPagina = "Estudios";
        }

        private void CargaGrid()
        {
            ArrayList parametros = new ArrayList();
            ConsultaServiceReference.ConsultaServiceClient servicio = new ConsultaServiceReference.ConsultaServiceClient();

            //Parámetros
            parametros.Add(CreaParametroConsulta("@pnIdLaboratorio", SessionLaboratorio.IdLaboratorio, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdEstudio", null, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@psNombreEstudio", txtNombre.Text, ConsultaServiceReference.Parameter.vEnumType.VarChar));

            ConsultaServiceReference.Parameter[] prms = new ConsultaServiceReference.Parameter[parametros.Count];
            parametros.CopyTo(prms);

            //Consulta
            gridEstudios.DataSource = servicio.EjecutaConsultaConParametros("spObtenEstudios", prms);
            gridEstudios.DataBind();
        }

        private void EliminaEstudio(int? IdEstudio)
        {
            try
            {
                EstudioServiceReference.EstudioServiceClient servicio = new EstudioServiceReference.EstudioServiceClient();
                EstudioServiceReference.Estudio est = new EstudioServiceReference.Estudio();
                est.IdEstudio = Convert.ToInt32(IdEstudio);
                est.IdLaboratorio = SessionLaboratorio.IdLaboratorio;
                servicio.EliminaEstudio(est);
                CargaGrid();
            }
            catch (Exception ex)
            {
                Master.alert(ex.Message);
            }
        }

        #endregion
    }
}